﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.ComponentModel;
using BusinessLogic.Model;
using Mvp.Comments;
using BusinessLogic;
using Mvp;

namespace WebControls
{
    public partial class CommentControl : BaseControl, ICommentControlView
    {
        public Comment Comment { get; set; }

        #region PageEvents

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Roles.IsUserInRole(RoleConstants.Admin))
            {
                pnlAdministration.Visible = true;
            }

            anchor.Name = UrlAnchorConstants.Comment + Comment.CommentID;
            lbPostAuthor.Text = Comment.AuthorUsername;
            lbPostDate.Text = Comment.PostDate.ToTimeAdjustedString();
            ltPostText.Text = Comment.Text;
            hlIPAddress.Text = String.IsNullOrEmpty(Comment.IPAddress) ? "Unknown" : Comment.IPAddress;
            hlIPAddress.NavigateUrl = "~/Admin/IPInfo.aspx?" + QueryStringConstants.IP + "=" + Comment.IPAddress;


            if (Comment.LastEditedDate != null)
            {
                //add last editor and last edited date
                lastEdit.Visible = true;
                lbLastEditDate.Text = Comment.LastEditedDate.Value.ToTimeAdjustedString();
                lbLastEditor.Text = Comment.LastEditedBy;
            }
        }

        #endregion

        #region ControlEvents

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(DeleteCommandName))
            {
                CommandEventArgs args = new CommandEventArgs(this.DeleteCommandName, this.DeleteCommandArgument);
                OnDeleteCommand(args);
            }
            else
            {
                throw new Exception("Delete command not provided");
            }
        }

        #endregion

        #region DeleteCommand

        private string deleteCommandName;

        [DefaultValue("")]
        public string DeleteCommandName
        {
            get
            {
                return deleteCommandName == null ? String.Empty : deleteCommandName;
            }
            set
            {
                deleteCommandName = value;
            }

        }

        public string deleteCommandArgument;

        [DefaultValue("")]
        public string DeleteCommandArgument
        {
            get
            {
                return deleteCommandArgument == null ? String.Empty : deleteCommandArgument;
            }
            set
            {
                deleteCommandArgument = value;
            }
        }

        protected static readonly object EventCommand = new object();

        public event CommandEventHandler Command
        {
            add
            {
                Events.AddHandler(EventCommand, value);
            }
            remove
            {
                Events.RemoveHandler(EventCommand, value);
            }
        }

        protected virtual void OnDeleteCommand(CommandEventArgs ce)
        {
            CommandEventHandler ceh = this.Events[EventCommand] as CommandEventHandler;
            if (ceh != null)
            {
                ceh(this, ce);
            }

            //This is the trick to cause Command event to bubble up
            base.RaiseBubbleEvent(this, ce);
        }


        #endregion

    }
}